local function upgrade(inst)


    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")
    local pt = Vector3(inst.Transform:GetWorldPosition())

    

    ----------------- 创建商人 只在洞外创建
    inst:DoTaskInTime(0.1,function()
        if TheWorld:HasTag("cave") then
            return
        end
        local npc_intermediary = TheSim:FindFirstEntityWithTag("monster_npc_pigman_intermediary_businessman")
        if npc_intermediary == nil then            
            local tar_pt = inst.npc_base_lib:GetSpawnPoint(inst,8)
            if tar_pt == nil then
                for i = 1, 100, 1 do
                    tar_pt = inst.npc_base_lib:GetSpawnPoint(inst,8)
                    if tar_pt ~= nil then
                        break
                    end
                end
            end
            if tar_pt == nil then
                tar_pt = pt
            end

            local points = inst.npc_base_lib:GetSurroundPoints({
                target = tar_pt,
                range = 1.5,
                num = 3
            })
            -----------------------------------------------------------------------------
            ------ 排序最远-最近的点
            local dist_point_table = {}
            for k, temp_point in pairs(points) do
                local temp_dis = inst.npc_base_lib:Dsitance_T2P(inst,temp_point)
                dist_point_table[temp_dis] = temp_point
            end

            local ret_pt = {}
            for i = 1, 10, 1 do
                local farthest_dist = 0
                for temp_dist, temp_point in pairs(dist_point_table) do
                    if temp_dist > farthest_dist and temp_point ~= nil then
                        farthest_dist = temp_dist
                    end
                end
                if farthest_dist ~= 0 then
                    table.insert(ret_pt,dist_point_table[farthest_dist])
                    dist_point_table[farthest_dist] = nil
                end
            end


            npc_intermediary = SpawnPrefab("monster_npc_pigman_intermediary_businessman")
            npc_intermediary.Ready = true
            npc_intermediary.Transform:SetPosition(ret_pt[1].x,0,ret_pt[1].z)
       
            local cart = SpawnPrefab("npc_item_recycle_cart")
            cart.Transform:SetPosition(ret_pt[2].x,0,ret_pt[2].z)
            cart.Ready = true

            local light = SpawnPrefab("npc_item_streetlight")
            light.Transform:SetPosition(ret_pt[3].x,0,ret_pt[3].z)
            light.Ready = true
            light:AddTag("INLIMBO")

        end
    end)

    ----------------- 创建 海盗笼子
    if TheWorld:HasTag("cave") and TheWorld.npc_everything_data:Get("woodlegs_out_of_cage.task_end") ~= true  then
        inst:DoTaskInTime(0.2,function()
            ----------------------------------------------------------------------------------------------------------------
            if TheWorld.npc_everything_data:Get("woodlegs_out_of_cage.create") ~= true then
                TheWorld.npc_everything_data:Set("woodlegs_out_of_cage.create",true)
                local tar_pt = inst.npc_base_lib:GetSpawnPoint(inst,8)
                if tar_pt == nil then
                    for i = 1, 100, 1 do
                        tar_pt = inst.npc_base_lib:GetSpawnPoint(inst,8)
                        if tar_pt ~= nil then
                            break
                        end
                    end
                end
                if tar_pt == nil then
                    tar_pt = pt
                end
                local cage = SpawnPrefab("npc_item_woodlegs_cage")
                cage.Transform:SetPosition(tar_pt.x, 0, tar_pt.z)
            end
            ----------------------------------------------------------------------------------------------------------------
        end)
    end


    
end


AddPrefabPostInit(
    "multiplayer_portal",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end
        upgrade(inst)        
    end
)
AddPrefabPostInit(
    "multiplayer_portal_moonrock",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end
        upgrade(inst)        
    end
)
AddPrefabPostInit(
    "multiplayer_portal_moonrock_constr",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end
        upgrade(inst)        
    end
)